package com.yt.user.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.db.utils.RedisTokenUtil;
import com.yt.user.models.bo.LoginInfoSaveBO;
import com.yt.user.models.service.ISysUserService;
import com.yt.user.models.vo.LoginInfoVO;
import com.yt.user.models.vo.SysUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yt.user.models.service.ILoginInfoService;
import com.yt.user.models.bo.LoginInfoBO;
import com.yt.user.models.dto.LoginInfoDTO;

import java.util.HashMap;

/**
 * 登录信息控制器
 *
 * @author lizicong
 * @Date 2025-09-05 15:14:07
 */

@RestController
@RequestMapping("/api/loginInfo")
@Slf4j
@Api(value = "LoginInfoApiController",tags = "登录信息Api接口")
public class LoginInfoApiController {

    @DubboReference
    private ILoginInfoService loginInfoService;

    @DubboReference
    private ISysUserService userinfoservice;

    @Autowired
    private RedisTokenUtil redisTokenUtil;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<LoginInfoDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        LoginInfoDTO dto = loginInfoService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "登录信息列表",notes = "登录信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<LoginInfoDTO>> queryPageList(LoginInfoBO bo){
        Wrapper<LoginInfoDTO> wrapper = new EntityWrapper<>();
                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<LoginInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = loginInfoService.selectPage(page, wrapper);

        return new Result<>(page);
    }
    @PostMapping("/login")
    @ApiOperation(value = "登录",notes = "登录")
    public Result<HashMap<String,Object>> login(@RequestBody LoginInfoSaveBO loginInfoSaveBO){
    HashMap<String,Object> map = new HashMap<>();
    if(null == loginInfoSaveBO){
        return new Result<>(CodeEnum.FAIL_100002);
    }
    LoginInfoVO userinfovo = loginInfoService.getLogin(loginInfoSaveBO);
    if(null == userinfovo){
        //账号不存在
       return new Result<>(CodeEnum.FAIL_USER_200003);
    }

    SysUserVO sysUserVO = loginInfoService.getUserlogin(loginInfoSaveBO);
    if(null == sysUserVO){
        //账号不存在
        return new Result<>(CodeEnum.FAIL_USER_200006);
    }
    //生成token并存入redis
    String token = redisTokenUtil.generateToken(sysUserVO.getUserName());
    sysUserVO.setToken(token);
    map.put("sysUserVO",sysUserVO);

    return new Result<>(map);
    }
}
